<?php

namespace app\admin\controller;
use think\Db;
use think\Request;

class Zans extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where = [];
            if(isset($user_name) && $user_name != ''){
                $where['v_title|pp_name|t_name|user_username|user_phone'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($typeid) && $typeid != ''){
                $where['pp_t_id'] = $typeid;
            }
            if(isset($s_pp_id) && $s_pp_id != ''){
                $where['v_pp_id'] = $s_pp_id;
            }
            if(session('agid') == 10){
                $where['v_pp_id'] = session('uid');
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_zan')->alias('z')
                ->join('up_pp_video v','z.zan_v_id=v.v_id','LEFT')
                ->join('up_u_user u','z.zan_user_id=u.user_id','LEFT')
                ->join('up_pp pp','v.v_pp_id=pp.pp_id','LEFT')
                ->join('up_types t','pp.pp_t_id=t.t_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $field = 'z.*,v_title,v_url,pp_name,t_name,user_username,user_phone';
            $lists = Db::table('up_zan')->alias('z')
                ->join('up_pp_video v','z.zan_v_id=v.v_id','LEFT')
                ->join('up_u_user u','z.zan_user_id=u.user_id','LEFT')
                ->join('up_pp pp','v.v_pp_id=pp.pp_id','LEFT')
                ->join('up_types t','pp.pp_t_id=t.t_id','LEFT')
                ->field($field)
                ->where($where)
                ->page($NowPage,$limits)
                ->order('zan_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $types = Db::table('up_types')
            ->field('t_id,t_name')
            ->order('t_sort desc')
            ->select();
        $pps = Db::table('up_pp')
            ->field('pp_id,pp_name')
            ->order('pp_sort desc')
            ->select();
        $this->assign('types',$types);
        $this->assign('pps',$pps);
        return $this->fetch();
    }
}